﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Super_Pong
{
    public class ComputerPlayer : Bar
    {

        public ComputerPlayer(ContentManager content, GameWindow window, String texture_asset_name, String left_or_right, Vector2 initialSpeed)
        {
            setTexture(content.Load<Texture2D>(texture_asset_name));
            if (left_or_right == "left")
            {
                setPos(new Vector2(0, window.ClientBounds.Height / 2 - getTexture().Height / 2));
            }
            if (left_or_right == "right")
            {
                setPos(new Vector2(window.ClientBounds.Width - getTexture().Width, window.ClientBounds.Height / 2 - getTexture().Height / 2));
            }
            setSpd(initialSpeed);
        }

        public enum CPU
        {
            Easy,
            Normal,
            Hard
        }

        CPU Difficulty;

        public void ComputerDifficulty(Ball ball, GameWindow window, GameTime gameTime)
        {
            switch (Difficulty)
            {
                case CPU.Easy:
                    if (Math.Abs(ball.getPos().X - getPos().X) < window.ClientBounds.Width / 2)
                    {
                        if (ball.getPos().Y + ball.getTexture().Height / 2 > getPos().Y + getTexture().Height / 2)
                        {
                            setPos(getPos() + getSpd() * (float)gameTime.ElapsedGameTime.TotalSeconds);
                        }
                        if (ball.getPos().Y + ball.getTexture().Height / 2 < getPos().Y + getTexture().Height / 2)
                        {
                            setPos(getPos() - getSpd() * (float)gameTime.ElapsedGameTime.TotalSeconds);
                        }
                    }
                    else
                    {
                        if (ball.getPos().Y + ball.getTexture().Height / 2 > getPos().Y + getTexture().Height / 2)
                        {
                            setPos(getPos());
                        }
                        if (ball.getPos().Y + ball.getTexture().Height / 2 < getPos().Y + getTexture().Height / 2)
                        {
                            setPos(getPos());
                        }
                    }
                    break;
                case CPU.Normal:
                    if (Math.Abs(ball.getPos().X - getPos().X) < window.ClientBounds.Width / 2)
                    {
                        if (ball.getPos().Y + ball.getTexture().Height / 2 < getPos().Y + getTexture().Height / 2)
                            setPos(getPos() - getSpd());
                        else if (ball.getPos().Y + ball.getTexture().Height / 2 > getPos().Y + +getTexture().Height / 2)
                            setPos(getPos() + getSpd());
                    }
                    break;
                case CPU.Hard:
                    if (Math.Abs(ball.getPos().X - getPos().X) < window.ClientBounds.Width / 2)
                    {
                        if (ball.getPos().Y + ball.getTexture().Height / 2 < getPos().Y + getTexture().Height / 2)
                            setPos(getPos() - getSpd());
                        else if (ball.getPos().Y + ball.getTexture().Height / 2 > getPos().Y + +getTexture().Height / 2)
                            setPos(getPos() + getSpd());
                    }
                    break;
            }
        }

        public void update(GameTime time, GameWindow window, Ball ball)
        {
            positionLimit(window);
            ComputerDifficulty(ball, window, time);
        }



    }

}
